var $;
var layer;
var form;

    layui.use(['layer','form','layedit'], function () {
        layer = layui.layer;
        $ = layui.jquery;
        form = layui.form;
        layedit = layui.layedit;

        var layedit_index=layedit.build('textsId'); //建立编辑器


        //初始化分类名称
            getclass();
            function getclass(){
                var url=location.search;
                var id=url.substring((location.search.indexOf("="))+1,url.length);
                if(id!=''&&id!=null){
                    $.post("/tea/selcourseClassName", {classId: id}, function (res) {
                        if (res.code>-1) {
                            $("#className").val(res.obj);
                        } else {
                            layer.msg("分类数据加载失败", {icon: 2});
                        }
                    });
                }
            };


        //选择分类
        $("#classimg").on("click",function () {
            layer.open({
                type:2,
                title:"课程分类",
                area:['300px','400px'],
                shade:0.4,
                offset:'auto',
                content:"TeaCourseClass.html",
                success:function(layero,index) {},
                btn:['确定','取消'],
                yes:function(index,layero){
                    var obj=layero.find('iframe').contents();
                    var classId=obj.find("#classId").val();//课程分类
                    $("#classId").val(classId);
                    $.post("/tea/selcourseClassName", {classId: classId}, function (res) {
                        if(res){
                            $("#className").val(res.obj);
                            layer.close(index);
                        }else{
                            layer.msg("数据加载失败",{icon:2});
                        }
                    });
                },
                btn2:function (index,layero) {
                    layer.close(index);
                }
            });
        });

        //添加草稿
        $("#addCMC").on("click",function () {
            console.log($("#classId").val());
            getFrom(2);
        });

        //发布文章
        $("#addMC").on("click",function () {
            getFrom(5);
        });

        //提交数据
        function getFrom(type) {
            var id=$("#microcircleId").val();
            //获得富文本的内容
            var layeditContent=layedit.getContent(layedit_index);
            var title=$("#articleTitle").val();
            var classId=$("#classId").val();
            var userId=$("#userId").val();
            if(title!=''&&classId!=''&&layeditContent!=''){
                if(id){
                    $.ajax({
                        url:"/tea/updMicroCircle",
                        type:"post",
                        dataType:"json",
                        contentType:"application/json",
                        data:JSON.stringify({"id":id,"title":title,"classId":classId,"texts":layeditContent,"details":type}),
                        success:function(data){
                            if (data.code>-1) {
                                if(type==2){
                                    layer.msg("成功保存", {icon: 1});
                                }else if(type==5) {
                                    layer.msg("文章添加成功", {icon: 1}, function () {
                                        var layer_3 = parent.layer_3;
                                        layer_3.close(layer_3.index);
                                    });
                                }
                            } else {
                                layer.msg("数据加载失败", {icon: 2});
                            }
                        },
                        error: function () {
                            layer.msg('数据获取失败！');
                        }
                    });
                }else{
                    $.ajax({
                        url:"/tea/addMicroCircle",
                        type:"post",
                        dataType:"json",
                        contentType:"application/json",
                        data:JSON.stringify({"uId":userId,"title":title,"classId":classId,"texts":layeditContent,"details":type}),
                        success:function(data){
                            if (data.code>-1) {
                                $("#microcircleId").val(data.obj);
                                if(type==2){
                                    layer.msg("成功保存", {icon: 1});
                                }else if(type==5){
                                    layer.msg("文章添加成功", {icon: 1}, function(){
                                        var layer_3=parent.layer_3;
                                        layer_3.close(layer_3.index);
                                    });
                                }
                            } else {
                                layer.msg("数据加载失败", {icon: 2});
                            }
                        },
                        error: function () {
                            layer.msg('数据获取失败！');
                        }
                    });
                }
            }else{
                layer.msg("请补完信息再保存哦",{icon:2});
            }
        }
    });